package com.demo.userprofile.component.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.springframework.stereotype.Service;

/**
 * 测试连接Hive代码
 *
 * @author userprofile_demo
 */
@Service
public class HiveService {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

    // 连接Hive执行简单查询
    public void testHive() {
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection(
                    "jdbc:hive2://userprofile-master:10000", "root", "");
            Statement stmt = con.createStatement();
            String sql = "select id from default.test limit 10";
            ResultSet res = stmt.executeQuery(sql);
            while (res.next()) {
                System.out.println(res.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
